package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工模块")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("登陆")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }



    @GetMapping
    @ApiOperation("获得用户")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "name",value = "书籍名字")
    )
    public Result getuser(String name){
        return Result.success();
    }


    @PostMapping
    @ApiOperation("新增员工")
    public Result insertUser(@RequestBody EmployeeDTO dto){
        log.info("员工信息：{}",dto.toString());

////        先查后插
//        String username = dto.getUsername();
//        Employee  employee=employeeService.getEmployeeByUsername(username);
//
//        if (employee!=null){
////                    说明存在，直接报错
//            return Result.error(username+"已存在，请换一个！");
//        }


        employeeService.insertUser(dto);

        return Result.success();



    }

    @GetMapping("/page")
    @ApiOperation("员工分页查询")
    public Result pageByCondition(EmployeePageQueryDTO queryDTO){

        log.info("name：{}，page：{}，pageSize：{}",queryDTO.getName(),queryDTO.getPage(),queryDTO.getPageSize());



        PageResult pageResult=    employeeService.pageByCondition(queryDTO);

        return Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("修改状态")
    public Result  changeStatus(@PathVariable Integer status,Long id){
        log.info("状态值:{},员工id：{}",status,id);

        employeeService.changeStatus(status,id);


        return Result.success("操作成功！");
    }


    @GetMapping("/{id}")
    @ApiOperation("通过Id获得员工")
    public Result getById(@PathVariable  Long id){

        Employee byId = employeeService.getById(id);

        return Result.success(byId);
    }


    @PutMapping
    @ApiOperation("修改员工")
    public Result update(@RequestBody EmployeeDTO employeeDTO){

        log.info("员工信息：{}",employeeDTO.toString());

        employeeService.update(employeeDTO);


        return Result.success();




    }






}
//1. 一边开发代码，一边配置注解，自动生成接口文档和测试平台


//1. 介绍一下后端请求的问题--nginx

